setwd("~/Cline_disp_evol/Balkau_orig/PBS")

s<-seq(0.1, 0.9, 0.1)
m<-seq(0.1, 0.4, 0.1)
r<-c(0.1, 0.3, 0.5)

script.file<-'~/evo-dispersal/Disp_clines/Balkau_orig.R'

for (ss in 1:length(s)) {
  for (mm in 1:length(m)){
    for (rr in 1:length(r)){
    
    	fid<-(ss-1)*length(m)*length(r)+(mm-1)*length(r)+rr-1
	    ##create the sh file
	    zz = file(paste("Balkau_o", fid,'.sh',sep=''),'w')
  	  cat('##################################\n',file=zz)
	    cat('#!/bin/sh\n',file=zz)
	    cat('cd $PBS_O_WORKDIR\n',file=zz)
  	  cat("R CMD BATCH --no-save --no-restore '--args s=", s[ss], " m=", m[mm], " r=", r[rr],
         " ID=",fid, "' ", sep="", file=zz)
	    cat(script.file, " ", paste("Balkau_o", fid,'.Rout',sep=''), "\n", sep="",file=zz)
	    cat('##################################\n',file=zz)
  	  close(zz)
			
    	#submit the job
	    system(paste("qsub -m n ", paste("Balkau_o", fid,".sh",sep=""),sep=""))
    }   
  }
}
